﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.Data.SqlTypes;
using W.Common.Enum;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.BinLocationCommands;
using W.Warehouse.Read.Api.Application.Command.StorageCommands;
using W.Warehouse.Read.Api.Application.CommandHandlder.BinLocationCommandsHandlder;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.StorageCommandsHandlder
{
    public class StorageSelectCommandsHandlder : IRequestHandler<StorageSelectCommands, ApiResult<StoragePageDTO>>
    {
        private readonly ILogger<StorageSelectCommandsHandlder> _logger;
        private readonly IStorageRepository _storageRepository;
        private readonly IStorageProductsRepository _storageProductsRepository;
        public StorageSelectCommandsHandlder(ILogger<StorageSelectCommandsHandlder> logger, IStorageRepository storageRepository, IStorageProductsRepository storageProductsRepository)
        {
            _logger = logger;
            _storageRepository = storageRepository;
            _storageProductsRepository = storageProductsRepository;
        }
        public async Task<ApiResult<StoragePageDTO>> Handle(StorageSelectCommands request, CancellationToken cancellationToken)
        {
            ApiResult<StoragePageDTO> result = new ApiResult<StoragePageDTO>();
            try
            {
                _logger.LogInformation("显示入库查询分页列表成功");
                StoragePageDTO storage = new StoragePageDTO();
                var list = from a in await _storageRepository.GetAllAsync()
                           join b in await _storageProductsRepository.GetAllAsync() on a.StorageId equals b.StorageId
                           where a.IsDelete == false && b.IsDelete == false
                           select new StorageDTO
                           {
                               StorageId = a.StorageId,
                               StorageProductsId = b.StorageProductsId,
                               StorageNo = a.StorageNo,
                               ProductName = b.ProductName,
                               ProductNo = b.ProductNo,
                               Batch = b.Batch,
                               ProductSpec = b.ProductSpec,
                               StorageNum = b.StorageNum,
                               BinLocationName = b.BinLocationName,
                               StorageType = a.StorageType,
                               VendorName = a.VendorName,
                               SingleMaker = a.SingleMaker,
                               StorageStatus = a.StorageStatus,
                               StorageStatusTest = a.StorageStatus.ToString(),
                               ExaminePeople = a.ExaminePeople,
                               ExamineTime = a.ExamineTime,
                               CreateName = a.CreateName,
                               IsDelete = a.IsDelete
                           };
                if (!string.IsNullOrEmpty(request.storageNo))
                {
                    list = list.Where(c => c.StorageNo == request.storageNo).ToList();
                }
                if (!string.IsNullOrEmpty(request.storageType))
                {
                    list = list.Where(c => c.StorageType == request.storageType).ToList();
                }
                if (request.storageStatus>0)
                {
                    list = list.Where(x => x.StorageStatus == (AuditStatus)request.storageStatus).ToList();
                }
               
                storage.TotalCount = list.Count();
                storage.TotalPage = (int)Math.Ceiling(storage.TotalCount * 1.0 / request.pageSize);
                storage.List = list.OrderByDescending(x => x.StorageId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = storage;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
